<!-- 课时信息 -->
<template>
  <div class="courseInfo">
    <el-dialog
      title="课时基本信息"
      :visible.sync="dialogVisible"
      width="40%"
      :before-close="dialogClose">
      <el-form
        :model="form"
        :rules="rules"
        ref="form"
        size="small"
        label-width="120px">
        <el-form-item label="课程名称">
          <el-input v-model="courseName" disabled></el-input>
        </el-form-item>
        <el-form-item label="章节名称">
          <el-input v-model="sectionName" disabled></el-input>
        </el-form-item>
        <el-form-item label="课时名称" prop="theme">
          <el-input v-model="form.theme"></el-input>
        </el-form-item>
        <el-form-item label="时长" prop="duration">
          <el-input-number v-model="form.duration" :min="0">
            <template slot="appent">分钟</template>
          </el-input-number>
        </el-form-item>
        <el-form-item label="是否开放试听" prop="isFree">
          <el-switch
            v-model="form.isFree"
            active-color="#40586f"
            inactive-color="#dcdfe6"
            :active-value="true"
            :inactive-value="false">
          </el-switch>
        </el-form-item>
        <el-form-item label="课程排序" prop="orderNum">
          <el-input v-model="form.orderNum">
            <template slot="appent">数字控制排序,数字越大越靠后</template>
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogClose" size="small">取 消</el-button>
        <el-button type="primary" @click="onSave" size="small">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getCourseById } from '@/services/courses'
import { getBySectionId, saveOrUpdateLesson } from '@/services/course-sections'
export default {
  name: 'courseInfo',
  props: {
    courseId: {
      type: [String, Number]
    }
  },
  data () {
    return {
      // 课程名称
      courseName: '',
      sectionName: '',
      dialogVisible: false,
      form: {
        courseId: '',
        sectionId: ''
      },
      rules: {
        theme: [
          { required: true, message: '请填写课时名称', trigger: blur }
        ],
        duration: [
          { required: true, message: '请填写时长', trigger: blur }
        ]
      }
    }
  },
  created () {
    this.getCourseById()
  },
  methods: {
    dialogClose () {
      this.dialogVisible = false
      this.form = {
        courseId: '',
        sectionId: ''
      }
    },
    dialogOpen () {
      this.dialogVisible = true
    },
    // 通过id获取课程信息
    async getCourseById () {
      const { data } = await getCourseById({ courseId: this.courseId })
      if (data.code === '000000') {
        this.courseName = data.data.courseName
      }
    },
    // 通过id获取章节信息
    async getBySectionName (sectionId) {
      const { data } = await getBySectionId({ sectionId: sectionId })
      if (data.code === '000000') {
        this.sectionName = data.data.sectionName
      }
    },
    // 添加课时
    courseAdd (data) {
      console.log(data)
      this.dialogOpen()
      this.sectionName = data.sectionName
      this.form.courseId = data.courseId
      this.form.sectionId = data.id
    },
    // 保存课时信息
    async onSave () {
      this.$refs.form.validate()
      const { data } = await saveOrUpdateLesson(this.form)
      if (data.code === '000000') {
        console.log(data)
        this.$message.success(`${this.form.id ? '更新' : '保存'}成功`)
        this.dialogClose()
        this.$emit('refresh')
      }
    },
    // 编辑课时
    courseEdit (data) {
      console.log(data)
      this.dialogOpen()
      // 通过点击编辑后,将章节的id传递过去,发送请求
      this.getBySectionName(data.sectionId)
      this.form = data
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
